package com.qd.qx.ui.module3;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aspsine.irecyclerview.universaladapter.ViewHolderHelper;
import com.aspsine.irecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import com.bumptech.glide.Glide;
import com.qd.common.base.BaseFragment;
import com.qd.common.baseapp.AppConfig;
import com.qd.common.baserx.RxSubscriber;
import com.qd.common.commonutils.ImageLoaderUtils;
import com.qd.common.commonutils.JsonUtils;
import com.qd.common.commonutils.ToastUitl;
import com.qd.common.commonwidget.LoadingDialog;
import com.qd.qx.R;
import com.qd.qx.api.Api;
import com.qd.qx.bean.BannerBean;
import com.qd.qx.bean.GongzuotaiBean;
import com.qd.qx.bean.MessageInfoBean;
import com.qd.qx.ui.main.activity.WebViewActivity;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Code is far away from bug with the animal protecting
 * Author：ys on 2018/7/13
 * Function：工作台模块
 */
public class WorkBenchFragment extends BaseFragment {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.rcvTop)
    RecyclerView rcvTop;
    @BindView(R.id.rcvBottom)
    RecyclerView rcvBottom;
    @BindView(R.id.xbanner)
    XBanner banner;
    CommonRecycleViewAdapter topAdapter;
    CommonRecycleViewAdapter bottomAdapter;
    List<GongzuotaiBean> topList = new ArrayList<>();
    List<GongzuotaiBean> bottomList = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_workbench_layout;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("工作台");
        requestBanner();

        topList.add(new GongzuotaiBean(R.drawable.da_ka_gzt, "打卡"));
        topList.add(new GongzuotaiBean(R.drawable.shen_pi_gzt, "审批"));
        topList.add(new GongzuotaiBean(R.drawable.qing_jia_gzt, "请假"));
        topList.add(new GongzuotaiBean(R.drawable.you_jian_gzt, "邮件"));
        topList.add(new GongzuotaiBean(R.drawable.gong_zuo_quan_gzt, "工作圈"));
        topList.add(new GongzuotaiBean(R.drawable.gengduo_icon, "更多"));
        initTopAdapter();
        bottomList.add(new GongzuotaiBean(R.drawable.xie_cheng_gzt, "携程"));
        bottomList.add(new GongzuotaiBean(R.drawable.di_di_gzt, "滴滴"));
        initBottomAdapter();

    }

    private void initBanner(BannerBean bannerBean) {
        if (null == bannerBean.getData()) {
            return;
        }
        // urlList.add("http://pic.58pic.com/58pic/12/73/37/68R58PICZ8Q.jpg");
//        urlList.add("https://p.pstatp.com/weili/bl/57458192753493335.jpg");
//        urlList.add("https://p.pstatp.com/weili/bl/57398819125600860.jpg");
//        urlList.add("https://p.pstatp.com/weili/bl/57427964773664482.jpg");
        ArrayList<BannerBean.DataBean> bannerList = new ArrayList<>();
        bannerList.addAll(bannerBean.getData());
        //添加轮播图片数据（图片数据不局限于网络图片、本地资源文件、View 都可以）,刷新数据也是调用该方法
        banner.setData(bannerList, null);//第二个参数为提示文字资源集合
        //加载广告图片
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                BannerBean.DataBean bean = (BannerBean.DataBean) model;
                ImageLoaderUtils.displayRound(getActivity(), (ImageView) view, bean.getPicPath());
            }
        });
        banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                BannerBean.DataBean bean = (BannerBean.DataBean) model;
                switch (bean.getClickEventType()) {
                    case "1"://1 跳转H5
                        WebViewActivity.startAction(getActivity(), bean.getTitle(), bean.getPagePath());
                        break;
                    case "2"://2原生跳转
                        WebViewActivity.startAction(getActivity(), bean.getTitle(), bean.getPagePath());
                        break;
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        banner.stopAutoPlay();
    }

    private void initTopAdapter() {
        topAdapter = new CommonRecycleViewAdapter<GongzuotaiBean>(getActivity(), R.layout.gzt_item_layout) {
            @Override
            public void convert(ViewHolderHelper helper, GongzuotaiBean bean) {
                ImageView imageView = helper.getView(R.id.ivImg);
                TextView textView = helper.getView(R.id.tvName);
                textView.setText(bean.getItemName());
                imageView.setImageResource(bean.getDrawResource());

            }
        };
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rcvTop.setLayoutManager(gridLayoutManager);
        topAdapter.addAll(topList);
        rcvTop.setAdapter(topAdapter);

    }


    private void initBottomAdapter() {
        bottomAdapter = new CommonRecycleViewAdapter<GongzuotaiBean>(getActivity(), R.layout.gzt_item_layout) {
            @Override
            public void convert(ViewHolderHelper helper, final GongzuotaiBean bean) {
                ImageView imageView = helper.getView(R.id.ivImg);
                TextView textView = helper.getView(R.id.tvName);
                imageView.setImageResource(bean.getDrawResource());
                textView.setText(bean.getItemName());
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (bean.getItemName()) {
                            case "滴滴":
                                WebViewActivity.startAction(getActivity(), "滴滴", "https://www.didiglobal.com/");
                                break;
                            case "携程":
                                WebViewActivity.startAction(getActivity(), "携程", "http://www.ctrip.com/?sid=153507&allianceid=5376&ouid=title");
                                break;
                        }
                    }
                });
            }
        };
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rcvBottom.setLayoutManager(gridLayoutManager);
        bottomAdapter.addAll(bottomList);
        rcvBottom.setAdapter(bottomAdapter);

    }


    /**
     * 请求banner
     */
    private void requestBanner() {
        Map<String, String> map = new HashMap<>();
        map.put("postion", "1");//轮播图位置 1：App工作台
        mRxManager.add((Api.getDefault().getBanner("1"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<MessageInfoBean>(getActivity(), false) {
                    @Override
                    protected void _onNext(MessageInfoBean messageInfoBean) {
                        BannerBean bannerBean = (BannerBean) JsonUtils.ObjectToBean(messageInfoBean, BannerBean.class);
                        if (null != bannerBean && 0 != bannerBean.getData().size()) {
                            initBanner(bannerBean);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUitl.showShort("请求失败" + message);
                    }
                }));

    }
}
